package com.jishe.controller;
import com.jishe.pojo.User;
import com.jishe.service.UserService;
import com.jishe.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.jishe.pojo.Result;
import java.util.HashMap;
import java.util.Map;
@Slf4j
@RestController
public class loginController {
    @Autowired
    private UserService userService;
    @RequestMapping("/login")
    public Result login(@RequestBody User user){
        log.info("用户登录：{}",user);
        User u = userService.login(user);
        //登陆成功，生成令牌并且下发令牌
        if(u != null){
            Map<String, Object> claims=new HashMap<>();
            claims.put("id",u.getId());
            claims.put("username",u.getUsername());
            String jwt = JwtUtils.generateJwt(claims);
            return Result.success(jwt);
        }
        //登陆失败，返回错误信息
        else {
            return Result.error("用户名或密码错误");
        }
    }
}
